home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / answers / reshape.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  8.2 KB  |  356 lines

  1. /*
  2.  * Copyright 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* reshape.c - open a window and clear the background.
  19.  *    Set up callbacks to handle keyboard input.
  20.  *      Model some objects.  Use perspective projection.
  21.  *    Use independent modeling transformations to position objects.
  22.  *    Added reshape callback to reset the viewport and viewing volume.
  23.  *
  24.  *    F1 key            - print help information
  25.  *    SPACE key        - generates a random background color
  26.  *    <s> key            - toggle smooth/flat shading
  27.  *    Escape Key        - exit program
  28.  */
  29. #include <GL/gl.h>
  30. #include <GL/glu.h>
  31. #include <GL/glut.h>
  32.  
  33. #include <stdio.h>
  34. #include <math.h>
  35.  
  36. /* Function Prototypes */
  37.  
  38. GLvoid initgfx( GLvoid );
  39. GLvoid keyboard( GLubyte, GLint, GLint );
  40. GLvoid specialkeys( GLint, GLint, GLint );
  41. GLvoid reshape( GLsizei, GLsizei );
  42. GLvoid drawScene( GLvoid );
  43.  
  44. void checkError( char * );
  45. void printHelp( char * );
  46.  
  47. /* Global Variables */
  48.  
  49. static char *progname; 
  50.  
  51. /* Global Definitions */
  52.  
  53. #define KEY_ESC    27    /* ascii value for the escape key */
  54.  
  55. void
  56. main( int argc, char *argv[] )
  57. {
  58.     GLsizei width, height;
  59.  
  60.     glutInit( &argc, argv );
  61.  
  62.     /* create a window that is 1/4 the size of the screen,
  63.      * and position it in the middle of the screen.
  64.      */
  65.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  66.     height = glutGet( GLUT_SCREEN_HEIGHT );
  67.     glutInitWindowPosition( width / 4, height / 4 );
  68.     glutInitWindowSize( width / 2, height / 2 );
  69.     glutInitDisplayMode( GLUT_RGBA );
  70.     glutCreateWindow( argv[0] );
  71.  
  72.     initgfx();
  73.  
  74.     glutReshapeFunc( reshape );
  75.     glutKeyboardFunc( keyboard );
  76.     glutSpecialFunc( specialkeys );
  77.     glutDisplayFunc( drawScene ); 
  78.  
  79.     progname = argv[0];
  80.  
  81.     printHelp( progname );
  82.  
  83.     glutMainLoop();
  84. }
  85.  
  86. void
  87. printHelp( char *progname )
  88. {
  89.     fprintf(stdout, 
  90.         "\n%s - create a scene and handle resizing\n\n"
  91.         "F1 key        - print help information\n"
  92.         "SPACE Key    - generates a random background color\n"
  93.         "<s> key        - toggle smooth/flat shading\n"
  94.         "Escape Key    - exit the program\n\n",
  95.         progname);
  96. }
  97.  
  98. GLvoid
  99. initgfx( GLvoid )
  100. {
  101.     /* set clear color to blue */
  102.     glClearColor( 0.0, 0.0, 1.0, 1.0 );
  103. }
  104.  
  105. GLvoid 
  106. reshape( GLsizei width, GLsizei height )
  107. {
  108.     GLdouble    aspect;
  109.  
  110.     glViewport( 0, 0, width, height );
  111.  
  112.     /* compute aspect ratio */
  113.     aspect = (GLdouble) width / (GLdouble) height;
  114.  
  115.     glMatrixMode( GL_PROJECTION );
  116.  
  117.     /* Reset world coordinates first ... */
  118.     glLoadIdentity();
  119.  
  120.     /* Reset the viewing volume based on the new aspect ratio */
  121.     gluPerspective( 45.0, aspect, 3.0, 7.0 );
  122.  
  123.     glMatrixMode( GL_MODELVIEW );
  124. }
  125.  
  126. void 
  127. checkError( char *label )
  128. {
  129.     GLenum error;
  130.     while ( (error = glGetError()) != GL_NO_ERROR )
  131.         printf( "%s: %s\n", label, gluErrorString(error) );
  132. }
  133.  
  134. GLvoid 
  135. keyboard( GLubyte key, GLint x, GLint y )
  136. {
  137.     static GLboolean flat = GL_FALSE;
  138.  
  139.     switch (key) {
  140.     case ' ':    /* SPACE key */
  141.         /* generate a random background color */
  142.         glClearColor( drand48(), drand48(), drand48(), 1.0 ); 
  143.         glutPostRedisplay();
  144.         break;
  145.     case 's':    /* s key */
  146.         /* toggle between smooth and flat shading */
  147.         flat = !flat;
  148.         if (flat)
  149.             glShadeModel( GL_FLAT );
  150.         else 
  151.             glShadeModel( GL_SMOOTH );
  152.         glutPostRedisplay();
  153.         break;
  154.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  155.         exit(0);
  156.     }
  157. }
  158.  
  159. GLvoid 
  160. specialkeys( GLint key, GLint x, GLint y )
  161. {
  162.     switch (key) {
  163.     case GLUT_KEY_F1:    /* Function key #1 */
  164.         /* print help information */
  165.         printHelp( progname );
  166.         break;
  167.     }
  168. }
  169.  
  170. GLvoid
  171. drawWindow()
  172. {
  173.     /* window */
  174.     static GLfloat v0[] = { 0.0, 0.4 };
  175.     static GLfloat v1[] = { 0.0, 0.0 };
  176.     static GLfloat v2[] = { 0.1, 0.4 };
  177.     static GLfloat v3[] = { 0.1, 0.0 };
  178.     static GLfloat v4[] = { 0.2, 0.4 };
  179.     static GLfloat v5[] = { 0.2, 0.0 };
  180.  
  181.     /* draw 2 quadrilateral strip to make a window */
  182.     glBegin( GL_QUAD_STRIP );
  183.         glColor3f( 1.0, 0.0, 0.0 );
  184.         glVertex2fv (v0);
  185.         glColor3f( 0.9, 0.0, 1.0 );
  186.         glVertex2fv (v1);
  187.         glColor3f( 0.8, 0.1, 0.0 );
  188.         glVertex2fv (v2);
  189.         glColor3f( 0.7, 0.2, 1.0 );
  190.         glVertex2fv (v3);
  191.         glColor3f( 0.6, 0.3, 0.0 );
  192.         glVertex2fv (v4);
  193.         glColor3f( 0.5, 0.4, 1.0 );
  194.         glVertex2fv (v5);
  195.         glColor3f( 0.4, 0.5, 0.0 );
  196.     glEnd();
  197. }
  198.  
  199. /* draw a "circle" using a triangle fan; set the
  200.  * center of the circle to white, and the outside
  201.  * to the color passed in
  202.  */
  203. GLvoid
  204. drawFan( GLfloat *color )
  205. {
  206.     /* Draw a triangle fan centered a the current coordinate
  207.      * system origin 
  208.      */
  209.     glBegin( GL_TRIANGLE_FAN );
  210.         glColor3f( 1.0, 1.0, 1.0 );
  211.         glVertex2f( 0.0, 0.0 );
  212.         glColor3fv( color );
  213.         glVertex2f( 0.0, -0.2 );
  214.         glVertex2f( 0.2, -0.1 );
  215.         glVertex2f( 0.2, 0.1 );
  216.         glVertex2f( 0.0, 0.2 );
  217.         glVertex2f( -0.2, 0.1 );
  218.         glVertex2f( -0.2, -0.1 );
  219.         glVertex2f( 0.0, -0.2 );
  220.     glEnd();
  221. }
  222.  
  223. /* draw a flower with the base of the stem 
  224.  * at the current location 
  225.  */
  226. GLvoid
  227. drawFlower( GLfloat *color )
  228. {
  229.     static GLfloat darkgreen[] = { 0.0, 0.25, 0.0 };
  230.     /* draw the stem with 2 leaves */
  231.  
  232.     glColor3fv( darkgreen );
  233.     glBegin( GL_LINES );
  234.         glVertex2f( 0.0, 0.0 );         /* stem */
  235.         glVertex2f( 0.0, 0.25 );
  236.         glVertex2f( 0.0, 0.1 );         /* leaf */
  237.         glVertex2f( 0.05, 0.15 ); 
  238.         glVertex2f( 0.0, 0.05 );     /* leaf */
  239.         glVertex2f( -0.05, 0.2 );
  240.     glEnd();
  241.  
  242.     glPushMatrix();
  243.         /* move to the top of the stem */
  244.         glTranslatef( 0.0, 0.25, 0.0 );
  245.  
  246.         /* use a scaled triangle fan for the flower head */
  247.         glScalef( 0.1, 0.1, 1.0 );
  248.         drawFan( color );  
  249.     glPopMatrix();
  250. }
  251.  
  252. /* draw a house centered at the current origin;
  253.  * the bounding box for the house is 1.0 x 2.0 units
  254.  */
  255. GLvoid
  256. drawHouse()
  257. {
  258.     /* draw the house */
  259.     glColor3f( 1.0, 1.0, 1.0 ); /* white */
  260.     glRectf( -0.5, -0.75, 0.5, 0.75 );
  261.  
  262.     /* draw the door */
  263.     glColor3f( 0.5, 0.2, 0.1 ); /* brown */
  264.     glRectf( -0.2, -0.75, 0.2, 0.0 );
  265.  
  266.     glPushMatrix();
  267.         /* move to the top left corner of the house */
  268.         glTranslatef( -0.5, 0.75, 0.0 );
  269.  
  270.         /* draw a triangle for the roof */
  271.         glColor3f( 0.0, 0.0, 0.0 ); /* black */
  272.         glBegin( GL_TRIANGLES );
  273.             glVertex2f( 0.0, 0.0 );
  274.             glVertex2f( 1.0, 0.0 );
  275.             glVertex2f( 0.5, 0.5 );
  276.         glEnd();
  277.     glPopMatrix();
  278.  
  279.     glPushMatrix();
  280.         /* move to the location for the left window */
  281.         glTranslatef( -0.4, 0.2, 0.0 );
  282.         drawWindow();
  283.     glPopMatrix();
  284.  
  285.     glPushMatrix();
  286.         /* move to the location for the right window */
  287.         glTranslatef( 0.2, 0.2, 0.0 );
  288.         drawWindow();
  289.     glPopMatrix();
  290. }
  291.  
  292. GLvoid
  293. drawScene( GLvoid )
  294. {
  295.     static GLfloat red[] = { 1.0, 0.0, 0.0 };
  296.     static GLfloat magenta[] = { 1.0, 0.0, 1.0 };
  297.     static GLfloat yellow[] = { 1.0, 1.0, 0.0 };
  298.     static GLfloat blue[] = { 0.0, 0.0, 1.0 };
  299.     static GLfloat green[] = { 0.0, 1.0, 0.0 };
  300.     static GLfloat darkgreen[] = { 0.0, 0.25, 0.0 };
  301.  
  302.     glClear( GL_COLOR_BUFFER_BIT );
  303.  
  304.     glPushMatrix();
  305.  
  306.         /* move inside the viewing volume */
  307.         glTranslatef( 0.0, 0.0, -4.0 );
  308.  
  309.         /* draw the ground in different shades of green */
  310.         glBegin( GL_POLYGON );
  311.             glColor3fv( green );
  312.             glVertex2f( -3.0, -2.0 );
  313.             glVertex2f( 3.0, -2.0 );
  314.             glColor3fv( darkgreen );
  315.             glVertex2f( 3.0, 0.0 );
  316.             glVertex2f( -3.0, 0.0 );
  317.         glEnd();
  318.  
  319.         /* draw the house */
  320.         glPushMatrix();
  321.             /* move to where the center of the house should be */
  322.             glTranslatef( -0.5, 0.0, 0.0 );
  323.  
  324.             /* shrink the house slightly */
  325.             glScalef( 0.7, 0.7, 0.7 );
  326.             drawHouse();
  327.         glPopMatrix();
  328.  
  329.         /* draw the sun */
  330.         glPushMatrix();
  331.             /* move to the location of the sun */
  332.             glTranslatef( 2.0, 2.0, -1.5 );
  333.             glColor3fv( yellow );
  334.             glutSolidSphere( 0.2, 8, 15 );
  335.         glPopMatrix();
  336.  
  337.         /* draw several tulips in the foreground */
  338.         glPushMatrix();
  339.             glTranslatef( 1.0, -1.0, 1.0 );
  340.             drawFlower( red );
  341.             glTranslatef( 0.0, 0.0, -0.5 );
  342.             drawFlower( yellow );
  343.         glPopMatrix();
  344.         glPushMatrix();
  345.             glTranslatef( -1.0, -1.0, 1.0 );
  346.             drawFlower( blue );
  347.             glTranslatef( 0.0, 0.0, -0.5 );
  348.             drawFlower( magenta );
  349.         glPopMatrix();
  350.  
  351.     glPopMatrix();
  352.  
  353.     checkError( "drawScene" );
  354.     glFlush();
  355. }
  356.